package com.atguigu.tingshu.search.service.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.query.album.AlbumStatVo;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {
    @Autowired
    private AlbumFeignClient albumFeignClient;
    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    private Executor threadPoolTaskExecutor;

    /**
     * 查询专辑详情
     *
     * @param albumId 专辑Id
     * @return map 专辑详情集合: albumInfo , albumStatVo , baseCategoryView , announcer
     */
    @Override
    public Map<String, Object> albumInfo(Long albumId, Long userId) {
        ConcurrentHashMap<String, Object> albumInfoVoMap = new ConcurrentHashMap<>();
        //todo:  调用OpenFeign接口,封装以下信息 albumInfo , albumStatVo , baseCategoryView , announcer

        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
            Assert.notNull(albumInfo, "专辑内容为空");
            albumInfoVoMap.put("albumInfo", albumInfo);
            return albumInfo;
        }, threadPoolTaskExecutor);

        CompletableFuture<Void> albumStatCompletableFuture = CompletableFuture.runAsync(() -> {
            AlbumStatVo albumStatVo = albumFeignClient.getAlbumStatVo(albumId).getData();
            Assert.notNull(albumStatVo, "专辑统计内容为空");
            albumInfoVoMap.put("albumStatVo", albumStatVo);
        }, threadPoolTaskExecutor);

        CompletableFuture<Void> baseCategoryViewCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            BaseCategoryView baseCategoryView = albumFeignClient.getCategoryView(albumInfo.getCategory3Id()).getData();
            Assert.notNull(baseCategoryView, "专辑分类信息为空");
            albumInfoVoMap.put("baseCategoryView", baseCategoryView);
        }, threadPoolTaskExecutor);

        CompletableFuture<Void> announcerCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            UserInfoVo announcer = userFeignClient.getUserInfoVo(albumInfo.getUserId()).getData();
            Assert.notNull(announcer, "专辑主播信息为空");
            albumInfoVoMap.put("announcer", announcer);
        }, threadPoolTaskExecutor);

        CompletableFuture.allOf(albumInfoCompletableFuture,
                albumStatCompletableFuture,
                baseCategoryViewCompletableFuture,
                announcerCompletableFuture
        ).orTimeout(5, TimeUnit.SECONDS).join();
        return albumInfoVoMap;
    }
}
